﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Net.Sockets;
using System.IO;
using System.Resources;

namespace UDP_WPF
{
    public partial class Driver : Form
    {
        public static Driver static_driver;

        public static bool isconnect = false;
        public DataListener dataListener;
        //客户区移动
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;


        public Driver()
        {
            InitializeComponent();
            this.BackColor = Color.Bisque;
            this.TransparencyKey = Color.Bisque;  
            init(); //初始化一些数据
            static_driver = this;

       
        }
       

        private void init()
        {


            //SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            tb_action1.Text = "VK_ESC";
            tb_action2.Text = "VK_SPACE";
            tb_action3.Text = "VK_K";
            tb_action4.Text = "VK_J";


            tb_localport.Text = "8888";
            //按键映射
            tb_up.Text = "VK_UP";
            tb_up_value.Text = "2";

            tb_down.Text = "VK_DOWN";
            tb_down_value.Text = "2";

            tb_left.Text = "VK_LEFT";
            tb_left_value.Text = "2";

            tb_right.Text = "VK_RIGHT";
            tb_right_value.Text = "2";
        }
        //开始按钮
        private void btn_test_Click(object sender, EventArgs e)
        {
            if (!isconnect)
            {
                
                string port = tb_localport.Text;
                if(port != "")
                {
                    NetPara.PORT_LOCAL = int.Parse(port);
                }
                NetPara.IS_IMPLEMENTORDER = true;
                localipaddr.Text = GetLoaclIpAddr() + ":" + NetPara.PORT_LOCAL;
               

                //btn_test.BackgroundImage = Image.FromFile("rec/image/open_btn.png");
                btn_test.BackgroundImage = Properties.Resources.open_btn; 
                dataListener = new DataListener();
                dataListener.rec();
                isconnect = true;
            }
            else
            {
                btn_test.BackgroundImage = Properties.Resources.close_btn; 
                NetPara.IS_IMPLEMENTORDER = false;
                dataListener.stop();
                localipaddr.Text = "0.0.0.0:0000";
                clientippaddr.Text = "无服务";
                isconnect = false;
            }
            
        }
        //按钮 - 清空记录
        private void btn_clearrecord_Click(object sender, EventArgs e)
        {
            tb_printftest.Text = "";
     
        }
        //按钮 - 设置按键映射
        private void btn_setkeymap_Click(object sender, EventArgs e)
        {
            //string teststr1 = "上" + OrderCode.ACTION_SENSOR_YU_TO_KEY + OrderCode.PACTET_VAULE_SENSOR_YUP + "  -- 下" + OrderCode.ACTION_SENSOR_YD_TO_KEY + OrderCode.PACTET_VAULE_SENSOR_YDOWN + "  -- 左" + OrderCode.ACTION_SENSOR_XLEFT_TO_KEY + OrderCode.PACTET_VAULE_SENSOR_XLEFT + "  -- 右" + OrderCode.ACTION_SENSOR_XRIGHT_TO_KEY + OrderCode.PACTET_VAULE_SENSOR_XRIGHT;
            //MessageBox.Show(teststr1);

            string stryd = tb_down.Text;   //下
            string strydvalue = tb_down_value.Text;

            string stryu = tb_up.Text;  //上
            string stryuvalue = tb_up_value.Text;

            string strxl = tb_left.Text;  //左
            string strxlvalue = tb_left_value.Text;

            string strxr = tb_right.Text;  //右
            string strxrvalue = tb_right_value.Text;

            int itnyd = VrtualKeyMap.Instance().GetKeyMap(stryd);
            int itnyu = VrtualKeyMap.Instance().GetKeyMap(stryu);
            int itnxl = VrtualKeyMap.Instance().GetKeyMap(strxl);
            int itnxr = VrtualKeyMap.Instance().GetKeyMap(strxr);

            int itnydvalue = int.Parse(strydvalue);
            int itnyuvalue = int.Parse(stryuvalue);
            int itnxlvalue = int.Parse(strxlvalue);
            int itnxrvalue = int.Parse(strxrvalue);
            
            if((stryd == "") || (strydvalue == "") || (stryu == "") || (stryuvalue == "") || (strxl == "") || (strxlvalue == "") || (strxr == "") || (strxrvalue == ""))
            {
                MessageBox.Show("错误：不能有空");
                return;
            }
            if ((itnyd == -1) || (itnyu == -1) || (itnxl == -1) || (itnxr == -1))
            {
                MessageBox.Show("错误的按钮键值");
                return;
            }
            if ( ((itnydvalue > 10) || (itnydvalue < 0)) || ((itnyuvalue > 10) ||(itnyuvalue < 0))  || ((itnxlvalue > 10) || (itnxlvalue < 0)) || ((itnxrvalue> 10) || (itnxrvalue < 0)))
            {
                MessageBox.Show("错误的灵敏度值");
                return;
            }

            GeneralPara.ACTION_SENSOR_YU_TO_KEY = itnyu;   //上
            NetPacket.PACTET_SENSOR_VAULE_YUP = itnydvalue;

            GeneralPara.ACTION_SENSOR_YD_TO_KEY = itnyd;  //下
            NetPacket.PACTET_SENSOR_VAULE_YDOWN = -itnydvalue;   //负的

            GeneralPara.ACTION_SENSOR_XLEFT_TO_KEY = itnxl; //左
            NetPacket.PACTET_SENSOR_VAULE_XLEFT = -itnxlvalue;  //负的

            GeneralPara.ACTION_SENSOR_XRIGHT_TO_KEY = itnxr;  //右
            NetPacket.PACTET_SENSOR_VAULE_XRIGHT = itnxrvalue;


            string str = "" + NetPacket.PACTET_SENSOR_VAULE_YUP + "," + NetPacket.PACTET_SENSOR_VAULE_YDOWN + "," + NetPacket.PACTET_SENSOR_VAULE_XLEFT + "," + NetPacket.PACTET_SENSOR_VAULE_XRIGHT;
            DataSend.Instance().send_PACTET_MSGSTYLE_SET_SENSOR(str, NetPara.IPE_CLIENT);



            int intaction1 = VrtualKeyMap.Instance().GetKeyMap(tb_action1.Text); ;
            int intaction2 = VrtualKeyMap.Instance().GetKeyMap(tb_action2.Text);
            int intaction3 = VrtualKeyMap.Instance().GetKeyMap(tb_action3.Text);
            int intaction4 = VrtualKeyMap.Instance().GetKeyMap(tb_action4.Text);
            if ((intaction1 == -1) || (intaction2 == -1) || (intaction3 == -1) || (intaction4 == -1))
            {
                MessageBox.Show("错误的按钮键值");
                return;
            }
            GeneralPara.ACTION1_TO_KEY = intaction1;   //上
            GeneralPara.ACTION2_TO_KEY = intaction2;   //上
            GeneralPara.ACTION3_TO_KEY = intaction3;   //上
            GeneralPara.ACTION4_TO_KEY = intaction4;   //上


             MessageBox.Show("设置成功");

        }
        /// <summary>
        /// 功能函数
        /// </summary>
     
        //获取本地IP
        public string GetLoaclIpAddr()
        {
            string localipaddr = "0.0.0.0";
            foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
            {
                if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
                {
                   localipaddr  = _IPAddress.ToString();
                   NetPara.IPADDR_LOCAL = localipaddr;
                   break;
                }
            }
            return localipaddr;
        }

        //鼠标按下
        private void CreateFolder_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);//客户区移动
        }
        //鼠标点击节点
        private void NodeClick(object sender, TreeNodeMouseClickEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }


      

       
    }
}
